﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity;
using Microsoft.Practices.ObjectBuilder2;
using EntLibContrib.Resource.Providers;

namespace EntLibContrib.Resource.Configuration.Unity
{
	/// <summary>
	/// This type supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
	/// Represents the process to create the container policies required to create an <see cref="BinaryResourceProvider"/>.
	/// </summary>
	public sealed class BinaryResourceProviderPolicyCreator : IContainerPolicyCreator
	{
		#region Methods
		/// <summary>
		/// Adds the policies required to create the provider specified by <paramref name="configurationObject"/>
		/// to <paramref name="policyList"/>.
		/// </summary>
		/// <param name="policyList">The <see cref="T:Microsoft.Practices.ObjectBuilder2.IPolicyList"/> to which the policies must be added.</param>
		/// <param name="instanceName">The name to use for the instance's policies.</param>
		/// <param name="configurationObject">The <see cref="T:System.Configuration.ConfigurationElement"/> that describes a provider.</param>
		/// <param name="configurationSource">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource"/> from which additional configuration information
		/// should be retrieved if necessary.</param>
		void IContainerPolicyCreator.CreatePolicies(
			IPolicyList policyList,
			string instanceName,
			ConfigurationElement configurationObject,
			IConfigurationSource configurationSource)
		{
			BinaryResourceProviderData castConfigurationObject = (BinaryResourceProviderData)configurationObject;

			new PolicyBuilder<BinaryResourceProvider, BinaryResourceProviderData>(
					instanceName,
					castConfigurationObject,
					c => new BinaryResourceProvider(c.ResourcePathName, c.ResourceBaseName)
					).AddPoliciesToPolicyList(policyList);
		}
		#endregion
	}
}
